using System;
using System.ComponentModel;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using NBModular.Module.WeChat.Application.WeChatUserService;
using NBModular.Module.WeChat.Application.WeChatUserService.ViewModels;
using NBModular.Module.WeChat.Domain.WeChatUser.Models;

namespace NBModular.Module.WeChat.Web.Controllers
{
    [Description("微信用户信息管理")]
    public class WeChatUserController : ModuleController
    {
        private readonly IWeChatUserService _service;

        public WeChatUserController(IWeChatUserService service)
        {
            _service = service;
        }

        [HttpGet]
        [Description("查询")]
        public Task<IResultModel> Query([FromQuery]WeChatUserQueryModel model)
        {
            return _service.Query(model);
        }

        [HttpPost]
        [Description("添加")]
        public Task<IResultModel> Add(WeChatUserAddModel model)
        {
            return _service.Add(model);
        }

        [HttpDelete]
        [Description("删除")]
        public Task<IResultModel> Delete([BindRequired]Guid id)
        {
            return _service.Delete(id);
        }

        [HttpGet]
        [Description("编辑")]
        public Task<IResultModel> Edit([BindRequired]Guid id)
        {
            return _service.Edit(id);
        }

        [HttpPost]
        [Description("修改")]
        public Task<IResultModel> Update(WeChatUserUpdateModel model)
        {
            return _service.Update(model);
        }

        [HttpPost]
        [AllowAnonymous]
        [Description("授权获取微信用户信息")]
        public async Task<IResultModel> GetUserInfoByAuthAsync(string appid, string code) 
        {
            return await _service.GetUserInfoByAuthAsync(appid, code);
        }

        [HttpPost]
        [AllowAnonymous]
        [Description("通过Openid获取微信用户信息")]
        public async Task<IResultModel> GetUserInfoByOpenidAsync(string openid) 
        {
            return await _service.GetUserInfoByOpenidAsync(openid);
        }

        [HttpPost]
        [AllowAnonymous]
        [Description("获取微信JS-SDK配置")]
        public async Task<IResultModel> GetWxConfigAsync(string appid, string url)
        {
            return await _service.GetWxConfigAsync(appid, url);
        }
    }
}
